package com.cslg.questionnaires.service;

import com.cslg.questionnaires.entity.Question;
import com.cslg.questionnaires.entity.Teacher;
import com.cslg.questionnaires.mapper.QuestionFormMapper;
import com.cslg.questionnaires.mapper.QuestionMapper;
import com.cslg.questionnaires.mapper.TeacherMapper;
import com.cslg.questionnaires.utils.ServiceMessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class HomeService {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private QuestionFormMapper questionFormMapper;

    public Map<String, Object> getMyQuestionsById(int owerid,int page,String userType){
        int startline = (page-1)*10;
        List<Question> questionsList = questionMapper.getQuestionByOwerid(owerid,userType,startline);
        if (questionsList.size() == 0){
            return ServiceMessageBuilder
                    .buildMessage(400,"Nothing",null);
        }else{
            Date nowdate = new Date();
            List<Map> questionStateList = new ArrayList<>();
            for (int i = 0;i<questionsList.size();i++) {
                //先判断审核
                //再判断时间
                Question onequestion = questionsList.get(i);
                int isexamine = onequestion.getIsexamine();
                Map<String, Object> questionState = new HashMap<String, Object>();
                if (isexamine == 0) {
                    // 正在审核中
                    questionState.put("state", 0);
                    questionState.put("question", onequestion);
                } else if (isexamine == 9) {
                    // 驳回
                    questionState.put("state", 9);
                    questionState.put("question", onequestion);
                } else if (isexamine == 8){
                    // 草稿
                    questionState.put("state", 8);
                    questionState.put("question", onequestion);
                } else {
                    // 通过了审核
                    Date startdate = onequestion.getStarttime();
                    Date finishdate = onequestion.getFinishtime();
                    if (startdate.before(nowdate) && finishdate.after(nowdate)){
                        // 正在进行
                        questionState.put("state",1);
                        questionState.put("question",onequestion);
                    }else if (startdate.after(nowdate) || startdate.equals(nowdate)){
                        // 还未开始
                        questionState.put("state",2);
                        questionState.put("question",onequestion);
                    }else if (finishdate.before(nowdate) || finishdate.equals(nowdate)){
                        // 已经结束了
                        questionState.put("state",3);
                        questionState.put("question",onequestion);
                    }
                }
                questionStateList.add(questionState);
            }
            return ServiceMessageBuilder
                    .buildMessage(200,"OK",questionStateList);
        }
    }

    public HashMap<String,Object> delMyQuestionByid(int id,int userid,String userType){
        int questionRow = questionMapper.delQuestionById(id,userid,userType);
        int questionFormRow = questionFormMapper.delQuestionFormById(id);
        if (questionRow == 1){
            return ServiceMessageBuilder
                    .buildMessage(200,"success",null);
        }else {
            return ServiceMessageBuilder
                    .buildMessage(400,"error",null);
        }
    }
}
